package cn.netuo.bumbersoll;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import lib.grasp.adapter.BaseAdapter;

/**
 * 我的记录列表-功能项适配器
 */
public class SelectBleAdapter extends BaseAdapter<SelectBleAdapter.Holder, BluetoothDevice> {

    private Context mContext;
    private View.OnClickListener        mOnClickListener;

    public SelectBleAdapter(Context mContext, View.OnClickListener mOnClickListener) {
        this.mContext   = mContext;
        this.mOnClickListener = mOnClickListener;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_select_ble_item, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.loadData(position);
    }

    @Override
    public int getItemCount() {
        super.getItemCount();
        return getDatas().size();
    }

    class Holder extends RecyclerView.ViewHolder{
        private TextView tv_name;
        private TextView tv_id;

        Holder(View itemView) {
            super(itemView);
            tv_name     = itemView.findViewById(R.id.bt_name);
            tv_id       = itemView.findViewById(R.id.bt_id);
        }

        void loadData(int position) {
            BluetoothDevice bean = getDatas().get(position);
            String name = TextUtils.isEmpty(bean.getName())     ? "NULL" : bean.getName();
            String id   = TextUtils.isEmpty(bean.getAddress())  ? "NULL" : bean.getAddress();
            tv_name .setText(name);
            tv_id   .setText(id);

            if(mOnClickListener != null){
                itemView.setTag(bean);
                itemView.setOnClickListener(mOnClickListener);
            }
        }
    }
}
